<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="base/resource"/>
    <title>设备管理 - Big Whale</title>
    <link href="../../../libs/bootstrap-select/bootstrap-select.min.css" rel="stylesheet">
    <link href="../../../css/common.css" rel="stylesheet"/>
</head>
<body ng-app="content-app" ng-controller="content-controller">
<div class="modal fade" id="editDlg" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title" id="myModalLabel">{{editItem.id ? "编辑" : "新增"}}</h4>
            </div>
            <form class="form-horizontal" ng-submit="onSaveItem()">
                <div class="modal-body">
                    <div class="form-group">
                        <label for="modal_input_ip" class="col-sm-4 control-label">
                            IP&nbsp;<B>*</B>
                        </label>
                        <div class="col-sm-5">
                            <input id="modal_input_ip" class="form-control" ng-model="editItem.ip" required="required">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="modal_input_host" class="col-sm-4 control-label">
                            HOST&nbsp;<B>*</B>
                        </label>
                        <div class="col-sm-5">
                            <input id="modal_input_host" class="form-control" ng-model="editItem.host" required="required">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="modal_input_mac" class="col-sm-4 control-label">
                            MAC&nbsp;<B>*</B>
                        </label>
                        <div class="col-sm-5">
                            <input id="modal_input_mac" class="form-control" ng-model="editItem.mac" required="required">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="modal_select_clusterId" class="col-sm-4 control-label">
                            集群
                        </label>
                        <div class="col-sm-5">
                            <select id="modal_select_clusterId" class="form-control selectpicker show-tick"
                                    ng-model="editItem.clusterId" ng-options="item.id as item.name for item in clusterList">
                                <option value="">请选择</option>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="modal_select_status" class="col-sm-4 control-label">
                            状态
                        </label>
                        <div class="col-sm-5">
                            <select id="modal_select_status" class="form-control selectpicker show-tick"
                                    ng-model="editItem.status" ng-options="item.value as item.name for item in enableTypeList" required>
                            </select>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                    <button type="submit" class="btn btn-primary" id="btn_submit">提交</button>
                </div>
            </form>
        </div>
    </div>
</div>

<div class="container-fluid animated fadeInDown">
    <div class="bw-nav">
        <div class="row">
            <ol class="breadcrumb">
                <li>集群管理</li>
                <li class="active">设备管理</li>
            </ol>
            <a class="bw-refresh" onclick="location.reload()" title="刷新">
                <i class="fa fa-refresh" style="font-size: 18px"></i>
            </a>
        </div>
    </div>
    <div class="bw-body">
        <div class="row">
            <div class="col-md-12 ">
                <div class="col-md-12 search-form">
                    <form class="form-inline" ng-submit="options.mypage.toFirst()">
                        <div class="form-group">
                            <input type="text" class="form-control" placeholder="ip" ng-model="options.query.ip">
                        </div>
                        <div class="form-group">
                            <input type="text" class="form-control" placeholder="host" ng-model="options.query.host">
                        </div>
                        <div class="form-group">
                            <label for="query_select_clusterId">集群</label>
                            <select id="query_select_clusterId" class="selectpicker show-tick"
                                    ng-model="options.query.clusterId" ng-options="item.id as item.name for item in clusterList">
                                <option value="">不限</option>
                            </select>
                        </div>
                        <div class="form-group">
                            <button type="submit" class="btn btn-success">搜索</button>
                            <button type="button" class="btn btn-primary" ng-click="onNewItem()">新增</button>
                        </div>
                    </form>
                </div>
                <div class="col-md-12">
                    <div class="table-responsive">
                        <table class="table table-hover">
                            <thead>
                                <tr>
                                    <th>
                                        操作
                                    </th>
                                    <th>
                                        IP
                                    </th>
                                    <th>
                                        HOST
                                    </th>
                                    <th>
                                        MAC
                                    </th>
                                    <th>
                                        集群
                                    </th>
                                    <th>
                                        状态
                                    </th>
                                    <th>
                                        最后连接时间
                                    </th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr ng-repeat="item in options.items">
                                    <td>
                                        <p class="operate">
                                            <span ng-click="onEditItem(item)">
                                                <i class="glyphicon glyphicon-pencil" aria-hidden="true" title="编辑"></i>
                                            </span>
                                        </p>
                                    </td>
                                    <td>
                                        {{item.ip}}
                                    </td>
                                    <td>
                                        {{item.host}}
                                    </td>
                                    <td>
                                        {{item.mac}}
                                    </td>
                                    <td>
                                        {{clusterMap[item.clusterId].name}}
                                    </td>
                                    <td>
                                        <label ng-if="enableTypeMap[item.status]" class="label label-{{enableTypeMap[item.status].style}}">
                                            {{enableTypeMap[item.status].name}}
                                        </label>
                                    </td>
                                    <td>
                                        {{item.lastConnTime}}
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
                <mypage ng-model="options"></mypage>
                <div class="clearfix"></div>
            </div>
        </div>
    </div>
</div>
<script src="../../../libs/bootstrap-select/bootstrap-select.min.js" type="text/javascript"></script>
<script src="../../../libs/bootstrap-select/i18n/defaults-zh_CN.js" type="text/javascript"></script>
<script src="../../../libs/angular-1.3.9/angular.min.js" type="text/javascript"></script>
<script src="../../../libs/angular-1.3.9/myangular.js" type="text/javascript"></script>
<script src="../../../js/common.js" type="text/javascript"></script>
<script type="text/javascript">
    var app = angular.module('content-app', []);
    registerAuth(app);
    registerPage(app);

    app.controller('content-controller', function ($scope, $http, $timeout) {
        appendEnableType($scope);
        getCluster($scope, $http);
        getAuthUser($scope, $http);

        $scope.onNewItem = function () {
            $scope.editItem = {status: 1};
            $('#editDlg').modal();
        };

        $scope.onEditItem = function(item) {
            $scope.editItem = angular.copy(item);
            $('#editDlg').modal();
        };

        $('#editDlg').on('shown.bs.modal', function () {
            $('.selectpicker').selectpicker('refresh');
        });

        $scope.onSaveItem = function() {
            $('#btn_submit').attr('disabled', 'true');
            $http.post('./save.api', $scope.editItem)
                .success(function(data) {
                    $('#editDlg').modal('hide');
                    if (data.code === 0) {
                        swal('操作成功', data.msg, 'success');
                        $scope.reload();
                    } else {
                        swal('操作失败', data.msg, 'error');
                    }
                    $('#btn_submit').removeAttr('disabled');
                });
        };

        // 获取数据列表
        $scope.options = {
            query: {
                status: ''
            },
            listUrl: './getpage.api'
        };

        // 刷新
        $scope.reload = function () {
            $scope.options.mypage.reload();
        };

        $timeout(function () {
            $('.selectpicker').selectpicker('refresh');
        }, 3000);
    });
</script>
</body>
</html>